package ui.windows;

import entities.Shift;
import java.awt.Color;
import java.sql.Time;
import javax.swing.JOptionPane;
import ui.panels.TimeSlotManagementPanel;

/**
 * 
 * @author viev
 * AddShiftDialog provides a simple mechanism for the manager create new shift
 */
public class AddShiftDialog extends javax.swing.JDialog {

    /** Creates new form AddShiftDialog */
    public AddShiftDialog(RemedySchedulerFrame mainFrame, TimeSlotManagementPanel parentPanel) {
        this.mainFrame = mainFrame;
        this.parentPanel = parentPanel;
        initComponents();
        includeModel();
    }
    /**
     * Include model after load from database
     */
    private void includeModel() {
        lbCheckID.setText("");
        lbCheckName.setText("");
        lbCheckTime1.setText("");
        lbCheckTime2.setText("");
        lbCheckTime.setText("");
        checkID = false;
        checkName = false;
        checkStartTime = false;
        checkEndTime = false;
        checkTime = false;
    }

    /**
     * create new shift.
     */
    private void createShift() {
        checkID();
        checkName();
        checkStartTime();
        checkEndTime();
        checkTime();
        if(checkID && checkName && checkStartTime && checkEndTime && checkTime) {
            Shift record = new Shift();
            record.setShiftID(Integer.parseInt(txtID.getText()));
            record.setName(txtName.getText());
            record.setStartTime(new Time(Integer.parseInt(txtStartHour.getText()), 
                    Integer.parseInt(txtMinute1.getText()), 0));
            record.setEndTime(new Time(Integer.parseInt(txtEndHour.getText()), 
                    Integer.parseInt(txtMinute2.getText()), 0));
            boolean returnVal = parentPanel.createShift(record);
            if(returnVal) {
                close();
            } else {
                JOptionPane.showMessageDialog(mainFrame, "Cannot insert !", "Error", JOptionPane.WARNING_MESSAGE);
            }
        }
    }

    /**
     * Check value of shiftID
     */
    private void checkID() {
        String id = txtID.getText();
        if (id != null && id.length() != 0) {
            int sid;
            try {
                sid = Integer.parseInt(id);
                if(sid <= 0)
                    throw new Exception();
                if (!parentPanel.containtID(sid)) {
                    lbCheckID.setText("valid");
                    lbCheckID.setForeground(new Color(102, 102, 102));
                    checkID = true;
                } else {
                    lbCheckID.setText("existed !");
                    lbCheckID.setForeground(Color.red);
                    checkID = false;
                }
            } catch (Exception e) {
                lbCheckID.setText("invalid !");
                lbCheckID.setForeground(Color.red);
                checkID = false;
            }
        } else {
            lbCheckID.setText("enter !");
            lbCheckID.setForeground(Color.blue);
            checkID = false;
        }
    }

    /**
     * Check value of Shift name
     */
    private void checkName() {
        String name = txtName.getText();
        if (name != null && name.length() != 0) {
            if (!parentPanel.containtName(name)) {
                lbCheckName.setText("valid");
                lbCheckName.setForeground(new Color(102, 102, 102));
                checkName = true;
            } else {
                lbCheckName.setText("existed !");
                lbCheckName.setForeground(Color.red);
                checkName = false;
            }
        } else {
            lbCheckName.setText("enter !");
            lbCheckName.setForeground(Color.blue);
            checkName = false;
        }
    }

    /**
     * Check value of start time.
     */
    private void checkStartTime() {
        String hour = txtStartHour.getText();
        if (hour != null && hour.length() != 0) {
            int startHour;
            try {
                startHour = Integer.parseInt(hour);
                if (startHour < 0 || startHour > 23) {
                    throw new Exception();
                }
                String minute1s = txtMinute1.getText();
                if (minute1s != null && minute1s.length() != 0) {
                    int minute1;
                    try {
                        minute1 = Integer.parseInt(minute1s);
                        if (minute1 < 0 || minute1 > 59) {
                            throw new Exception();
                        }
                        lbCheckTime1.setText("valid");
                        lbCheckTime1.setForeground(new Color(102, 102, 102));
                        checkStartTime = true;
                    } catch (Exception e) {
                        lbCheckTime1.setText("invalid !");
                        lbCheckTime1.setForeground(Color.red);
                        checkStartTime = false;
                    }
                } else {
                    lbCheckTime1.setText("enter minute !");
                    lbCheckTime1.setForeground(Color.blue);
                    checkStartTime = false;
                }
            } catch (Exception e) {
                lbCheckTime1.setText("invalid !");
                lbCheckTime1.setForeground(Color.red);
                checkStartTime = false;
            }
        } else {
            lbCheckTime1.setText("enter hour !");
            lbCheckTime1.setForeground(Color.blue);
            checkStartTime = false;
        }
    }

    /**
     * Check value of end time.
     */
    private void checkEndTime() {
        String hour = txtEndHour.getText();
        if (hour != null && hour.length() != 0) {
            int endHour;
            try {
                endHour = Integer.parseInt(hour);
                if (endHour < 0 || endHour > 23) {
                    throw new Exception();
                }
                String minute2s = txtMinute2.getText();
                if (minute2s != null && minute2s.length() != 0) {
                    int minute2;
                    try {
                        minute2 = Integer.parseInt(minute2s);
                        if (minute2 < 0 || minute2 > 59) {
                            throw new Exception();
                        }
                        lbCheckTime2.setText("valid");
                        lbCheckTime2.setForeground(new Color(102, 102, 102));
                        checkEndTime = true;
                    } catch (Exception e) {
                        lbCheckTime2.setText("invalid !");
                        lbCheckTime2.setForeground(Color.red);
                        checkEndTime = false;
                    }
                } else {
                    lbCheckTime2.setText("enter minute !");
                    lbCheckTime2.setForeground(Color.blue);
                    checkEndTime = false;
                }
            } catch (Exception e) {
                lbCheckTime2.setText("invalid !");
                lbCheckTime2.setForeground(Color.red);
                checkEndTime = false;
            }
        } else {
            lbCheckTime2.setText("enter hour !");
            lbCheckTime2.setForeground(Color.blue);
            checkEndTime = false;
        }
    }
    
    /**
     * Check valid of time, about start time and end time.
     */
    private void checkTime() {
        try {
            int startHour = Integer.parseInt(txtStartHour.getText());
            int endHour = Integer.parseInt(txtEndHour.getText());
            int minute1 = Integer.parseInt(txtMinute1.getText());
            int minite2 = Integer.parseInt(txtMinute2.getText());
            int startTime = startHour*60 + minute1;
            int endTime = endHour*60 + minite2;
            if(startTime < endTime) {
                checkTime = true;
                lbCheckTime.setText("");
            } else {
                checkTime = false;
                lbCheckTime.setText("invalid !");
                lbCheckTime.setForeground(Color.red);
            }
        } catch (Exception e) {
            checkTime = false;
        }
    }

    /**
     * close dialog.
     */
    private void close() {
        this.dispose();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        bClose = new javax.swing.JButton();
        bAdd = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtID = new javax.swing.JTextField();
        lbCheckID = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        lbCheckName = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtStartHour = new javax.swing.JTextField();
        txtEndHour = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        txtMinute1 = new javax.swing.JTextField();
        txtMinute2 = new javax.swing.JTextField();
        lbCheckTime1 = new javax.swing.JLabel();
        lbCheckTime2 = new javax.swing.JLabel();
        lbCheckTime = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Add New Shift");
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        bClose.setText("Close");
        bClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCloseActionPerformed(evt);
            }
        });

        bAdd.setText("Add");
        bAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAddActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(331, Short.MAX_VALUE)
                .addComponent(bAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bClose, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bClose)
                    .addComponent(bAdd))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setText("Create New Shift");

        jLabel2.setText("ID:");

        jLabel3.setText("Name:");

        jLabel4.setText("Start Time:");

        jLabel5.setText("End Time:");

        txtID.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtIDCaretUpdate(evt);
            }
        });

        lbCheckID.setFont(new java.awt.Font("Tahoma", 1, 11));
        lbCheckID.setText("value");

        txtName.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtNameCaretUpdate(evt);
            }
        });

        lbCheckName.setFont(new java.awt.Font("Tahoma", 1, 11));
        lbCheckName.setText("value");

        jLabel8.setForeground(new java.awt.Color(102, 102, 102));
        jLabel8.setText("hour:");

        jLabel9.setForeground(new java.awt.Color(102, 102, 102));
        jLabel9.setText("hour:");

        txtStartHour.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtStartHourCaretUpdate(evt);
            }
        });

        txtEndHour.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtEndHourCaretUpdate(evt);
            }
        });

        jLabel10.setForeground(new java.awt.Color(102, 102, 102));
        jLabel10.setText("minute:");

        jLabel11.setForeground(new java.awt.Color(102, 102, 102));
        jLabel11.setText("minute:");

        txtMinute1.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtMinute1CaretUpdate(evt);
            }
        });

        txtMinute2.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtMinute2CaretUpdate(evt);
            }
        });

        lbCheckTime1.setFont(new java.awt.Font("Tahoma", 1, 11));
        lbCheckTime1.setText("value");

        lbCheckTime2.setFont(new java.awt.Font("Tahoma", 1, 11));
        lbCheckTime2.setText("value");

        lbCheckTime.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbCheckTime.setText("value");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(55, 55, 55)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(45, 45, 45)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(txtID, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(35, 35, 35)
                                .addComponent(lbCheckID, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lbCheckName, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                                        .addComponent(jLabel8)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtStartHour, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                                        .addComponent(jLabel9)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(lbCheckTime)
                                            .addComponent(txtEndHour))))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(jLabel11)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtMinute2))
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(jLabel10)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtMinute1, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(lbCheckTime2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lbCheckTime1, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(46, 46, 46)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 398, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(34, 34, 34))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbCheckID))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbCheckName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel8)
                    .addComponent(txtStartHour, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(txtMinute1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbCheckTime1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel9)
                    .addComponent(txtEndHour, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11)
                    .addComponent(txtMinute2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbCheckTime2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbCheckTime)
                .addContainerGap(23, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void txtIDCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtIDCaretUpdate
    checkID();
}//GEN-LAST:event_txtIDCaretUpdate

private void txtNameCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtNameCaretUpdate
    checkName();
}//GEN-LAST:event_txtNameCaretUpdate

private void txtStartHourCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtStartHourCaretUpdate
    checkStartTime();
}//GEN-LAST:event_txtStartHourCaretUpdate

private void txtMinute1CaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtMinute1CaretUpdate
    checkStartTime();
}//GEN-LAST:event_txtMinute1CaretUpdate

private void txtEndHourCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtEndHourCaretUpdate
    checkEndTime();
}//GEN-LAST:event_txtEndHourCaretUpdate

private void txtMinute2CaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtMinute2CaretUpdate
    checkEndTime();
}//GEN-LAST:event_txtMinute2CaretUpdate

private void bAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAddActionPerformed
    createShift();
}//GEN-LAST:event_bAddActionPerformed

private void bCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCloseActionPerformed
    close();
}//GEN-LAST:event_bCloseActionPerformed
    private RemedySchedulerFrame mainFrame;
    private TimeSlotManagementPanel parentPanel;
    private boolean checkID, checkName, checkStartTime, checkEndTime, checkTime;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bAdd;
    private javax.swing.JButton bClose;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lbCheckID;
    private javax.swing.JLabel lbCheckName;
    private javax.swing.JLabel lbCheckTime;
    private javax.swing.JLabel lbCheckTime1;
    private javax.swing.JLabel lbCheckTime2;
    private javax.swing.JTextField txtEndHour;
    private javax.swing.JTextField txtID;
    private javax.swing.JTextField txtMinute1;
    private javax.swing.JTextField txtMinute2;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtStartHour;
    // End of variables declaration//GEN-END:variables
}
